import { createRouter, createWebHashHistory } from 'vue-router'
import { useUserStore } from '@/store/user'

import type { UserInfo } from '@/types/user'
import { addRoute } from '@/utils/permission'
import { getUserInfo } from '@/api/user'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        noLogin: true,
        name: '登录页'
      },
      component: () => import('@/views/login/index.vue')
    },
    {
      path: '/',
      name: 'layout',
      meta: {
        name: '首页'
      },
      component: () => import('@/views/layout/index.vue')
      // children: [
      //   {
      //     name: 'userList',
      //     path: 'user/list',
      //     meta: {
      //       isNeedLogin: true,
      //       title: '用户列表页'
      //     },
      //     component: () => import('@/views/User/index.vue')
      //   },
      //   {
      //     name: 'swiperList',
      //     path: 'swiper/list',
      //     meta: {
      //       isNeedLogin: true,
      //       title: '轮播图列表页'
      //     },
      //     component: () => import('@/views/Swiper/index.vue')
      //   },
      //   {
      //     name: 'goodsList',
      //     path: 'goods/list',
      //     meta: {
      //       isNeedLogin: true,
      //       title: '商品列表页'
      //     },
      //     component: () => import('@/views/Goods/index.vue')
      //   },
      //   {
      //     name: 'visibleEcharts',
      //     path: 'visible/echarts',
      //     meta: {
      //       isNeedLogin: true,
      //       title: '原生echarts'
      //     },
      //     component: () => import('@/views/Visible/echarts.vue')
      //   },
      //   {
      //     name: 'visibleEchartsThirds',
      //     path: 'visible/echarts-third',
      //     meta: {
      //       isNeedLogin: true,
      //       title: '第三方实现echarts'
      //     },
      //     component: () => import('@/views/Visible/echarts-other.vue')
      //   },
      //   {
      //     name: 'visibleSumbit',
      //     path: 'editor/submit',
      //     meta: {
      //       isNeedLogin: true,
      //       title: '富文本编辑器'
      //     },
      //     component: () => import('@/views/Editor/index.vue')
      //   },
      //   {
      //     name: 'mapBaidu',
      //     path: 'map/baidu',
      //     meta: {
      //       isNeedLogin: true,
      //       title: '百度地图原生'
      //     },
      //     component: () => import('@/views/Map/baidu.vue')
      //   },
      //   {
      //     name: 'mapBaiduThird',
      //     path: 'map/baidu-third',
      //     meta: {
      //       isNeedLogin: true,
      //       title: '百度地图第三方'
      //     },
      //     component: () => import('@/views/Map/baidu-vue3.vue')
      //   }
      // ]
    },
    {
      path: '/:pathMatch(.*)',
      meta: {
        name: '页面已丢失'
      },
      component: () => import('@/views/NotFound/index.vue')
    }
  ]
})

// 判断添加路由阀
export const operator = {
  isInit: true
}

router.beforeEach(async to => {
  //如果是不需要登陆就可以访问的页面就直接放行

  if (to.meta.noLogin) return true
  console.log(operator.isInit)
  const store = useUserStore()
  if (store.token) {
    if (operator.isInit) {
      // 添加动态路由
      console.log('进入到动态添加路由里了')

      // 调用用户登录接口，获取数据包括不同用户的路由权限并存到仓库里

      const res = await getUserInfo()
      if (res.data.code === 200) {
        store.setUserInfo(res.data.data)
      }
      router.removeRoute('/')
      router.addRoute({
        path: '/',
        name: 'layout',
        component: () => import('@/views/layout/index.vue')
      })
      addRoute(res.data.data.menus)

      // 语法中规定必须调用
      router.replace({ path: to.path })
      operator.isInit = false //将开关变为false，防止重复添加
      console.log('---动态添加的路由', router.getRoutes())
    }
    return true
  }

  // if (store.token) return true
  // 此时说明无token需要跳转到登录页

  // 通过query传参的方式将路径存储到url地址栏，记录跳转回登录页之前的路由路径，后续登录页登陆时取出路径参数作为跳转路由路径
  return { name: 'login', query: { redirect: to.fullPath } }
})

router.afterEach(to => {
  if (to.meta.name) {
    document.title = to.meta.name as string
  }
})

export default router
